{% extends 'base.html' %}

{% block content %}
<div class="container">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1>博客文章</h1>
        {% if current_user.is_authenticated %}
        <a href="{{ url_for('new_post') }}" class="btn btn-primary">写新文章</a>
        {% endif %}
    </div>

    <div class="row">
        <div class="col-md-8">
            {% for post in posts.items %}
            <article class="blog-post">
                <h2 class="blog-post-title">
                    <a href="{{ url_for('post', post_id=post.id) }}" class="text-dark">{{ post.title }}</a>
                </h2>
                <p class="blog-post-meta">
                    {{ post.created_at.strftime('%Y-%m-%d %H:%M') }} by {{ post.author.name }}
                    {% if post.category %}
                    <span class="badge badge-secondary">{{ post.category }}</span>
                    {% endif %}
                </p>
                <div class="blog-post-excerpt">
                    {{ post.content[:200] }}...
                </div>
                <a href="{{ url_for('post', post_id=post.id) }}" class="btn btn-link px-0">继续阅读</a>
            </article>
            {% endfor %}

            {% if posts.pages > 1 %}
            <nav aria-label="Page navigation">
                <ul class="pagination">
                    {% if posts.has_prev %}
                    <li class="page-item">
                        <a class="page-link" href="{{ url_for('blog', page=posts.prev_num) }}">上一页</a>
                    </li>
                    {% endif %}
                    
                    {% for page in posts.iter_pages() %}
                        {% if page %}
                            <li class="page-item {% if page == posts.page %}active{% endif %}">
                                <a class="page-link" href="{{ url_for('blog', page=page) }}">{{ page }}</a>
                            </li>
                        {% else %}
                            <li class="page-item disabled"><span class="page-link">...</span></li>
                        {% endif %}
                    {% endfor %}
                    
                    {% if posts.has_next %}
                    <li class="page-item">
                        <a class="page-link" href="{{ url_for('blog', page=posts.next_num) }}">下一页</a>
                    </li>
                    {% endif %}
                </ul>
            </nav>
            {% endif %}
        </div>

        <div class="col-md-4">
            <div class="card">
                <div class="card-header">
                    分类
                </div>
                <div class="list-group list-group-flush">
                    <a href="{{ url_for('blog') }}" class="list-group-item list-group-item-action">全部</a>
                    <a href="{{ url_for('blog', category='tech') }}" class="list-group-item list-group-item-action">技术</a>
                    <a href="{{ url_for('blog', category='movie') }}" class="list-group-item list-group-item-action">电影</a>
                    <a href="{{ url_for('blog', category='music') }}" class="list-group-item list-group-item-action">音乐</a>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %} 